package com.sjgtw.cloud.client.oauth.extractors;

import com.sjgtw.cloud.client.oauth.exceptions.OAuthException;
import com.sjgtw.cloud.client.oauth.model.Token;
import com.sjgtw.cloud.client.utils.HttpEncoder;
import com.sjgtw.cloud.client.utils.Preconditions;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Default implementation of {@AccessTokenExtractor}. Conforms to OAuth 2.0
 *
 */
public class TokenExtractor20Impl implements AccessTokenExtractor{
  private static final String TOKEN_REGEX = "access_token=([^&]+)";
  private static final String EMPTY_SECRET = "";

  /**
   * {@inheritDoc} 
   */
  public Token extract(String response){
    Preconditions.checkEmptyString(response, "Response body is incorrect. Can't extract a token from an empty string");

    Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
    if (matcher.find()){
        String token;
    	try {
    		token = HttpEncoder.decode(matcher.group(1));
    		return new Token(token, EMPTY_SECRET, response);
    	} catch (UnsupportedEncodingException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
          return null;
    } else{
      throw new OAuthException("Response body is incorrect. Can't extract a token from this: '" + response + "'", null);
    }
  }
}
